<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/3 0003
 * Time: 17:53
 */

/**
 * Redis单例对象
 */

namespace libs;

use think\Exception;

class Redis
{
	private static $_instance;

	const USER_REGISTER_LOCK = 'user_register_lock_%s';

	private function __construct()
	{
		try {
			self::$_instance = new \Redis();
			//redis配置
			$config = config('redis');
			self::$_instance->connect($config['host'], $config['port']);
			if ($config['password']) {
				self::$_instance->auth($config['password']);
			}
			if (!empty($config['db'])) {
				self::$_instance->select(intval($config['db']));
			}
		} catch (\Exception $e) {
			throw new Exception('Redis连接失败');
		}
	}

	//获取静态实例
	public static function getRedis()
	{
		if (!self::$_instance) {
			new self;
		}
		return self::$_instance;
	}

	/*
	 * 禁止clone
	 */
	private function __clone()
	{
	}
}
